package cn.edu.zjicm.wordsnet_d.data.test.selection;

import java.util.LinkedList;
import java.util.List;


import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.util.StringUtil;

public abstract class QuestionMaker implements IQuestionMaker {
	protected List<Integer> used = new LinkedList<Integer>();
	WordFactory wf;
	
	QuestionMaker(WordFactory wf){
		this.wf = wf;
	}

	public String getWrongChoice(OneWord word,int familiarDegree) {
		// TODO Auto-generated method stub
		String where1 = " Index_word_id!=" + word.getIndex_word_id();
		String where0 = " degree_fm=" + familiarDegree + " and " + where1;
		String where = where0 + " and senses like '%" + StringUtil.getType(word.getSensesSenior()) + "%'";
		int total = wf.getInt("select count(1) from Words_cet4 where " + where);
		if (total < 5) {
			where = where0;
			total = wf.getInt("select count(1) from Words_cet4 where " + where);
			if (total < 5) {
				where = where1;
				total = wf.getInt("select count(1) from Words_cet4 where " + where);
			}
		}
		OneWord wrong = null;
		for (int i = 0; i < 100 // 似乎有重复记录，避免死循环
				        && (wrong == null || used.contains(wrong.getIndex_word_id())); i++) {
			wrong = wf.getOneWordOfWhere(where + " order by degree_fm limit "
					+ WordFactory.rand(total) + ",1");
		}
		used.add(wrong.getIndex_word_id());
		// 从已学取
		// String sql = " from colloc_cet4 where Index_word_id_1="
		// + wrong.getIndex_word_id() + " or Index_word_id_2="+
		// wrong.getIndex_word_id();
		// total = wf.getInt("select count(Colloc_cn) "+sql);
		// int limit=((int) (Math.random() * total * 2) % total);
		//
		// String ret =
		// wf.getStringBySQL("select Colloc_eng "+sql+" limit "+limit+",1");
		if (this instanceof Lemma2Sence)
			return wrong.getSensesSenior();
		return wrong.getLemma();
	}
}